package impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import dto.CommandDto;
import model.Command;
import model.LinuxInfo;
import service.AnalysisXmlService;

/**
 * @author zhangLin
 */
public class AnalysisXmlServiceDom4jImpl implements AnalysisXmlService {
    @Override
    public CommandDto analysisXml(String filePath) {
        try {
            Document document = parse(new URL(filePath));
            CommandDto commandDto = bar(document);
            return commandDto;
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;
    }

    public CommandDto bar(Document document) throws DocumentException {
        Element root = document.getRootElement();
        //获取子节点
        List<LinuxInfo> linuxInfos = creatLinuxInfo(root.element("hosts").elementIterator("host"));
        List<Command> commands = creatCommandInfo(root.element("commands").elementIterator("command"));
        CommandDto commandDto = new CommandDto();
        commandDto.setHostsDto(linuxInfos);
        commandDto.setCommandsDto(commands);
        return commandDto;
    }


    private List<LinuxInfo> creatLinuxInfo(Iterator<Element> elementList){
        List<LinuxInfo> linuxInfos = Lists.newArrayList();
        while (elementList.hasNext()){
            LinuxInfo linuxInfo = new LinuxInfo();
            Element element = elementList.next();
            Attribute attributeIp = element.attribute("ip");
            linuxInfo.setIp(attributeIp.getValue());
            Attribute attributeName = element.attribute("name");
            linuxInfo.setUserName(attributeName.getValue());
            Attribute attributePwd = element.attribute("pwd");
            linuxInfo.setPassword(attributePwd.getValue());
            linuxInfos.add(linuxInfo);
        }
        return linuxInfos;
    }

    private List<Command> creatCommandInfo(Iterator<Element> elementList){
        List<Command> commands = Lists.newArrayList();
        while (elementList.hasNext()){
            Command command = new Command();
            Element element = elementList.next();
            Attribute attributeCom = element.attribute("com");
            if (attributeCom != null) {
                command.setCom(attributeCom.getValue());
            }
            Attribute attributeWatTime = element.attribute("watTime");
            if (attributeWatTime != null && !Strings.isNullOrEmpty(attributeWatTime.getValue())){
                command.setWatTime(Integer.valueOf(attributeWatTime.getValue()));
            }
            commands.add(command);
        }
        return commands;
    }
}
